package com.orbitdevs.testgame.graphics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class BaseSprite {
	public Sprite sprite;
	private float xPos;
	private float yPos;
	private float xSize, ySize;
	private float xVel, yVel;
	private boolean onBlock = false;
	private boolean rightBlocked = false;
	private boolean underBlock = false;
	private boolean leftBlocked = false;
	public boolean movingRight = false;
	public boolean movingLeft = false;
	public boolean jumped = true;
	public TextureRegion[][] textures;
	private Animation[] animations;
	private Animation currentAnimation;
	private float animationTime;
	private String prevState;
	

	public BaseSprite(TextureRegion[][] textures, int texNum, float x, float y, float xs, float ys, float xv, float yv) {
		this.xVel = xv;
		this.yVel = yv;
		this.xPos = x;
		this.yPos = y;
		this.xSize = xs;
		this.ySize = ys;
		this.textures = textures;
		this.sprite = new Sprite(textures[0][0]);
		this.sprite.setSize(xSize, ySize);
		this.sprite.setPosition(xPos, yPos);
		this.sprite.setOrigin(xSize / 2, ySize / 2);
		this.animationTime = 0f;
		this.animations = getAnimations(textures[0].length);
	}

	

	public void update(float dt) {
		if(!onBlock){
			addVel(0, -900, dt);
			jumped = false;
			if(prevState != "JUMPING"){
				setAnimation("JUMPING");
			}
			prevState = "JUMPING";
			
		}
		else{
			if(prevState != "WALKING"){
				setAnimation("WALKING");
			}
			prevState = "WALKING";

		}
		if(xVel == 0 && yVel == 0){
			if(prevState != "IDLE"){
				setAnimation("IDLE");
			}
			prevState = "IDLE";
		}
		animationTime += dt;
		sprite.setRegion(currentAnimation.getKeyFrame(animationTime, true));
		
		this.xPos += xVel * dt;
		this.yPos += yVel * dt;
		this.sprite.setPosition(xPos, yPos);
	}

	public void addVel(float x, float y, float dt) {
		this.xVel += x * dt;
		this.yVel += y * dt;
	}
	
	public void setRBlocked(boolean blocked, float x){
		this.rightBlocked = blocked;
		if(blocked){
			this.xPos = x - this.xSize;
			if(this.xVel > 0){
				this.xVel = 0;
			}
		}
	}
	public boolean getRBlocked(){
		return this.rightBlocked;
	}
	public void setLBlocked(boolean blocked, float x){
		this.leftBlocked = blocked;
		if(blocked){
			this.xPos = x;
			if(this.xVel < 0){
				this.xVel = 0;
			}
		}
	}
	public boolean getLBlocked(){
		return this.leftBlocked;
	}
	
	public void setAnimation(String id){
		if(id == "IDLE"){
			this.currentAnimation = animations[0];
		}
		if(id == "WALKING"){
			this.currentAnimation = animations[1];
		}
		if(id == "JUMPING"){
			this.currentAnimation = animations[2];
		}
	}
	
	public Animation[] getAnimations(int len){
		Animation[] temp = new Animation[len];
		for(int i = 0; i < len; i++){
			temp[i] = new Animation(0.5f, this.textures[i]);
		}
		return temp;
	}
	
	public void setOnBlock(boolean blocked, float y){
		this.onBlock = blocked;
		
		if(blocked){
			this.jumped = false;
			this.yPos = y;
			this.yVel = 0;
		}
		else if(!blocked){
		}
		
	}
	public boolean getOnBlock(){
		return this.onBlock;
	}
	
	public void setUnderBlock(boolean blocked, float y){
		this.underBlock = blocked;
		if(blocked){
			this.yPos = y - this.ySize;
			if(this.yVel > 0){
				this.yVel = 0;
			}
			
		}
	}
	public boolean getUnderBlock(){
		return this.underBlock;
	}
	
	
	
	
	
	
	
	
	public float[] getBLeft(float dt) {
		return new float[] { (xPos), (yPos + ySize / 9) };
	}

	public float[] getTLeft(float dt) {
		return new float[] { (xPos), (yPos + 8 * (xSize / 9)) };
	}

	public float[] getTRight(float dt) {
		return new float[] { (xPos + xSize), (yPos + 8 * (ySize / 9)) };
	}

	public float[] getBRight(float dt) {
		return new float[] { (xPos + xSize), (yPos + ySize / 9) };
	}

	public float[] getMTop(float dt) {
		return new float[] { (xPos + xSize / 2), (yPos + ySize) };
	}

	public float[] getMBot(float dt) {
		return new float[] { (xPos + xSize / 2), (yPos) };
	}


	
	public float[] colGetBLeft(float dt) {
		return new float[] { (xPos - 1), (yPos + ySize / 9) };
	}

	public float[] colGetTLeft(float dt) {
		return new float[] { (xPos - 1), (yPos + 8 * (xSize / 9)) };
	}

	public float[] colGetTRight(float dt) {
		return new float[] { (xPos + xSize + 1), (yPos + 8 * (ySize / 9)) };
	}

	public float[] colGetBRight(float dt) {
		return new float[] { (xPos + xSize + 1), (yPos + ySize / 9) };
	}

	public float[] colGetMTop(float dt) {
		return new float[] { (xPos + xSize / 2), (yPos + ySize + 1) };
	}

	public float[] colGetMBot(float dt) {
		return new float[] { (xPos + xSize / 2), (yPos - 1) };
	}

	public void moveX(float x) {
		this.xPos += x;
	}

	public void moveY(float y) {
		this.yPos += y;
	}

	public float getxPos() {
		return xPos;
	}

	public void setxPos(float xPos) {
		this.xPos = xPos;
	}

	public void setLeft(float leftPos) {
		this.xPos = leftPos;
	}

	public void setRight(float rightPos) {
		this.xPos = rightPos - this.xSize;
	}

	public float getyPos() {
		return yPos;
	}

	public void setyPos(float yPos) {
		this.yPos = yPos;
	}

	public float getxSize() {
		return xSize;
	}

	public void setxSize(float xSize) {
		this.xSize = xSize;
	}

	public float getySize() {
		return ySize;
	}

	public void setySize(float ySize) {
		this.ySize = ySize;
	}

	public float getxVel() {
		return xVel;
	}

	public void setxVel(float xVel) {
		this.xVel = xVel;
	}

	public float getyVel() {
		return yVel;
	}

	public void setyVel(float yVel) {
		this.yVel = yVel;
	}
}
